# -*- encoding:utf-8 -*-
import codecs
import sys
rootpath = sys.path.append("D:/OneDriveEdu/file/project/grpc_w2m_framework_m/worker/yuqing")
from worker.yuqing.crawler.TextRank import KeywordExtraction, SentenceExtraction


class Extractor(object):
    """中文文摘提取器"""

    def __init__(self, stop_words_file=None):
        super(Extractor, self).__init__()
        self.keyword_extraction = KeywordExtraction.KeywordExtraction(stop_words_file=stop_words_file)
        self.sentence_extraction = SentenceExtraction.SentenceExtraction(stop_words_file=stop_words_file)

    def keyword_train(self, text, num=10):
        self.keyword_extraction.train(text=text, window=2, lower=False, speech_tag_filter=True)
        keyword_res = self.keyword_extraction.get_keywords(num=num, word_min_len=2)
        keyphrase_res = self.keyword_extraction.get_keyphrases(keywords_num=20, min_occur_num=2)
        return keyword_res, keyphrase_res

    def sentence_train(self, text, sentences_percent='3%', sim_func='Standard'):
        self.sentence_extraction.train(text=text, lower=True, speech_tag_filter=True, source='all_filters',
                                       sim_func=sim_func)
        abstract = self.sentence_extraction.get_key_sentences(sentences_percent=sentences_percent)
        return abstract


if __name__ == '__main__':
    text = "8日23时35分，河北石家庄循环化工园区晋煤金石石家庄循环化工园区分公司合成车间甲醇回收装置发生爆裂，事故工厂出现滚滚浓烟，周边居民房屋出现不同程度受损。通报称事故系回收装置发生爆裂，经初步核查事故未造成人员伤亡。河北石家庄循环化工园区官网通报：2020年5月8日23时35分，河北石家庄循环化工园区晋煤金石石家庄循环化工园区分公司合成车间甲醇回收装置蒸汽汽包发生爆裂，园区主要领导和分管领导，以及应急、环保、消防、公安、交管等部门第一时间赶赴现场，经现场初步核查，无人员伤亡，未发生有毒可燃气体泄漏，周边企业未受到影响，现场情况稳定，正在进行后续处理，无次生灾害。"
    extractor = Extractor(stop_words_file='./TextRank/trainer/stopword_zh.data')
    keyword, keyphrase = extractor.keyword_train(text=text)
    # abstract = extractor.sentence_train(text, sentences_percent='20%')
    abstract = '。'.join(extractor.sentence_train(text, sentences_percent='50%')) + '。'
    print(abstract,"\n",keyword)

    # f = codecs.open('result_for_extractor.txt', 'w+', 'utf-8')
    #
    # f.write('keword:\n')
    # f.write('/'.join(keyword))
    #
    # f.write('\nkeyphrase:\n')
    # f.write('/'.join(keyphrase))
    #
    # f.write('\nabstract:\n')
    # f.write(u'。'.join(abstract) + u'。')
    # f.close()
